package com.zjgs.onlinepdfeditor.controller;

import com.zjgs.onlinepdfeditor.entity.FileEntity;
import com.zjgs.onlinepdfeditor.mapper.FileMapper;
import com.zjgs.onlinepdfeditor.tools.RequestMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class FileController {
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private RequestMessage requestMessage;
    @RequestMapping(method = RequestMethod.GET,value = "/getFiles")
    public List<FileEntity> getFiles() {
        List<FileEntity> files=fileMapper.getAll();
        return files;
    }

    @RequestMapping("/getFile")
    public FileEntity getFile(String id) {
        FileEntity file=fileMapper.getOne(id);
        return file;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/add")
    public void save(@RequestBody FileEntity file) {


        file.setFileUploadtime(getDateTime());
        file.setId(UUID.randomUUID().toString());
        fileMapper.insert(file);

    }

    @RequestMapping(method = RequestMethod.POST,value="/update")
    public RequestMessage update(@RequestBody  FileEntity file) {


           if( fileMapper.update(file)){
               requestMessage.setMessage("更新文件"+ file.getFileName() +"成功");
               requestMessage.setResult("true");

           }else {
               requestMessage.setMessage("更新文件"+ file.getFileName() +"失败");
               requestMessage.setResult("false");

           }

        return requestMessage;

    }

    @RequestMapping(value="/delete/{id}")
    public void delete(@PathVariable("id") String id) {
        fileMapper.delete(id);
    }

    public String getDateTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date());
    }
}
